<?php 

namespace Home\Controller;
use Vendor\Page;
class CategoryController extends ComController
{

	public function _initialize()
    {
        parent::_initialize();
    }

	/**
	 * 显示分类列表
	 * */
	public function getNovelList(){
        $this->is_session();
		$class_id = $_GET["class_id"] ? $_GET["class_id"] : 0;
		$this->assign("class_id", $class_id);
		
		$other_class_id= $_GET["other_class_id"] ? $_GET["other_class_id"] : 0;
		$this->assign("other_class_id", $other_class_id);
		
		$is_order= $_GET["is_order"] ? $_GET["is_order"] : 0;
		$this->assign("is_order", $is_order);
		
		//echo $other_class_id;
		
		if($other_class_id == 3)
			$this->assign("title", "男生");
		elseif($other_class_id == 4)
			$this->assign("title", "女生");
		else
			$this->assign("title", "排行");
		
		if($is_order == 5) {
			$this->assign("title", "限免");
			$this->assign("is_free", 1);
		}
		
		$this->display();
	}

	/**
	 * 获得所有类型
	 * */
	public function index(){
        $this->is_session();
		$class_list = D("novel_class")->where("is_other = 0")->select();
		// $list = D("Novel")->field("id,name as title,writer,description as summary,img as avatar")->order('click_count desc')->limit(0,10)->select();
		$this->assign("class_list",$class_list);
		// $this->assign("list",$list);
		$this->display();
	}
	
	/**
	 * 无刷新获得相对类型的小说列表
	 * */
	public function ajaxGetNovelList(){
		$data = $_POST;
		// var_dump($data);die;
		if(!empty($data['tid'])){ //普通类型
			$map["class_id"] = $data['tid'];
		}
		
		if(!empty($data['end'])){ //是否完结
			$map["is_end"] = $data['end'];
		}
		if(!empty($data['vip'])){ //特别类型
			$map["other_class_id"] = $data['vip'];
		}
		// $order_set = "id asc"; //热度排序
		// if(!empty($is_order)){
			$order_set = "click_count desc";
		// }
		
		//只显示上架的小说
		$map["is_shelf"] = 1;
		$limit = 10;
		$start = ($data['p']-1)*$limit;
		$list = D("Novel")->field("id,name as title,writer,description as summary,img as avatar")->order($order_set)->where($map)->limit($start.",".$limit)->select();
		// var_dump($list);die;
		if (empty($list)) {
			$datas =array('data'=>[],'info'=>"没有更多了","status"=>-21);
			$this->ajaxReturn($datas);
			exit;
		}
		foreach($list as $key=>$item){
			$list[$key]["summary"] = mb_substr($item["summary"],1,70, "UTF-8");
		}
		$file_host = C('FILE_HOST_PATH');
		foreach ($list as $v) {
			$writer = empty($v['writer'])?"佚名":$v['writer'];
			$item .='<div class="item"><a href='.C('URL').'/Home/Novel/novelinfo/id/'.$v['id'].'><img src="'.$v['avatar'].'" class="avatar"><div class="body" style="height:112px;"><span class="t">'.$v['title'].'</span><span class="author">'.$writer.'</span><span class="btn">立即阅读</span>
    			<p>'.$v['summary'].'</p></div></a></div>';
		}
		$item = str_replace("Array","",$item);
		$datas =array('data'=>$item,'info'=>"获取成功","status"=>1); 
		$this->ajaxReturn($datas);
	}	
	
}



